package com.doit.day06

import scala.util.Random

object _06_闭包 {
  def main(args: Array[String]): Unit = {
    //闭包：如果一个函数,访问到了它的外部（局部）变量的值，
    // 那么这个函数和他所处的环境,称为闭包
    /*    val num: Int = new Random().nextInt(10)

        val add = (a:Int)=>{
          a+num
        }*/


    val fx = (a: Int, b: Int) => {
      a + b
    }

    val fxx = (a: Int) => {
      val c = 20
      (b: Int) => a + b +c
    }




    val f = () => {
      var p = new Random().nextInt(10)
      val inner = () => {
        p += 1
        p
      }
      inner
    }

    val x1 = f()
    println(x1())
    println(x1())
    println(x1())
    println(x1())

    println("-----------")

    val x2 = f()
    println(x2())
    println(x2())
    println(x2())


  }
}
